﻿
using Estsh.Core.Dapper;
using Estsh.Core.Model.ExcelModel;
using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Model.Result;
using Estsh.Core.Models;
using Estsh.Core.Services;
using Estsh.Core.Util;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using System;
using System.Collections;




/***************************************************************************************************
 *
 * 更新人：sitong.dong
 * 描述：库位管理
 * 修改时间：2022.06.22
 * 修改日志：系统迭代升级
 *
 **************************************************************************************************/
namespace Estsh.Core.Wms.Services
{
    public class CycleCountManageService : BaseService<WmsCheckStock>, ICycleCountManageService
    {
        private readonly ICycleCountManageRepository repository;
        public CycleCountManageService(ICycleCountManageRepository _repository) : base(_repository)
        {
            repository = _repository;
        }

        public Hashtable getCheckStockByPage(string orderNo, string orderType, string orderStatus, string startTime, string endTime, string enabled, int factoryId, Pager pager, String direction, String sort)
        {
            Hashtable result = new Hashtable();

            String strWhere = " 1=1 ";
            if (orderNo != null && !orderNo.Trim().Equals(""))
            {
                strWhere += "and a.order_no like '%" + orderNo.Trim() + "%' ";
            }

            if (orderType != null && !orderType.Trim().Equals(""))
            {
                strWhere += "and a.order_type ='" + orderType.Trim() + "' ";
            }

            if (orderStatus != null && !orderStatus.Trim().Equals(""))
            {
                strWhere += "and a.order_status ='" + orderStatus.Trim() + "' ";
            }

            if (!String.IsNullOrEmpty(startTime) && !String.IsNullOrEmpty(endTime))
            {
                strWhere += " and  a.create_time BETWEEN '" + startTime.Trim() + "' AND '" + endTime.Trim() + "'";
            }
            else if (String.IsNullOrEmpty(startTime) && !String.IsNullOrEmpty(endTime))
            {
                strWhere += " and  a.create_time < '" + endTime.Trim() + "'";
            }
            else if (!String.IsNullOrEmpty(startTime) && String.IsNullOrEmpty(endTime))
            {
                strWhere += " and  a.create_time >'" + startTime.Trim() + "'";
            }

            if (enabled != null && !enabled.Trim().Equals(""))
            {
                strWhere += "and a.enabled = '" + enabled.Trim() + "' ";
            }
            strWhere += " and a.factory_id = " + factoryId + " ";
            String orderBy = "";
            if (sort != null && !"".Equals(sort.Trim()))
            {
                orderBy += typeof(WmsCheckStock).GetEntityColumnName(sort.Trim()) + " " + direction;
            }
            else
            {
                orderBy += typeof(WmsCheckStock).GetEntityColumnName("OrderNo") + " " + direction;
            }

            return repository.getCheckStockByPage(pager.pageSize, pager.pageNo, strWhere, orderBy);

        }
        public List<WmsCheckStockList_Extend> getCheckStockListByPage(string orderNo, string enabled, int factoryId, Pager pager, String direction, String sort)
        {
            String strWhere = " 1=1 ";
            if (orderNo != null && !orderNo.Trim().Equals(""))
            {
                strWhere += "and a.order_no = '" + orderNo.Trim() + "'";
            }
            strWhere += " and a.factory_id = " + factoryId + " ";
            return repository.getCheckStockListByPage(pager.pageSize, pager.pageNo, strWhere);
        }

        public List<WmsCheckStockList_Extend> getSearchCheckStockList(string orderNo, string partNo, string partSpec, string cartonNo)
        {
            String strWhere = " 1=1 ";
            if (orderNo != null && !orderNo.Trim().Equals(""))
            {
                strWhere += "and a.order_no = '" + orderNo.Trim() + "'";
            }
            if (partNo != null && !partNo.Trim().Equals(""))
            {
                strWhere += "and a.part_no like '%" + partNo.Trim() + "%'";
            }
            if (partSpec != null && !partSpec.Trim().Equals(""))
            {
                strWhere += "and a.part_spec like '%" + partSpec.Trim() + "%'";
            }
            if (cartonNo != null && !cartonNo.Trim().Equals(""))
            {
                strWhere += "and a.carton_no like '%" + cartonNo.Trim() + "%'";
            }

            return repository.getSearchCheckStockList(strWhere);
        }


        public List<WmsCheckStockPart> getCheckStockPartByPage(string orderNo, string enabled, int factoryId, Pager pager, String direction, String sort)
        {
            String strWhere = " 1=1 ";
            if (orderNo != null && !orderNo.Trim().Equals(""))
            {
                strWhere += "and a.order_no = '" + orderNo.Trim() + "'";
            }

            strWhere += " and a.factory_id = " + factoryId + " ";
            String orderBy = "";
            if (sort != null && !"".Equals(sort.Trim()))
            {
                orderBy += typeof(WmsCheckStockPart).GetEntityColumnName(sort.Trim()) + " " + direction;
            }
            else
            {
                orderBy += typeof(WmsCheckStockPart).GetEntityColumnName("orderNo") + " " + direction;
            }

            return repository.getCheckStockPartByPage(pager.pageSize, pager.pageNo, strWhere, orderBy);

        }


        public List<WmsCheckStockZone> getCheckStockZoneByPage(string orderNo, string enabled, int factoryId, Pager pager, String direction, String sort)
        {
            String strWhere = " 1=1 ";
            if (orderNo != null && !orderNo.Trim().Equals(""))
            {
                strWhere += "and a.order_no = '" + orderNo.Trim() + "'";
            }

            strWhere += " and a.factory_id = " + factoryId + " ";
            String orderBy = "";
            if (sort != null && !"".Equals(sort.Trim()))
            {
                orderBy += typeof(WmsCheckStockZone).GetEntityColumnName(sort.Trim()) + " " + direction;
            }
            else
            {
                orderBy += typeof(WmsCheckStockZone).GetEntityColumnName("ruid") + " " + direction;
            }

            return repository.getCheckStockZoneByPage(pager.pageSize, pager.pageNo, strWhere, orderBy);

        }

        public List<WmsCheckStockLocate> getCheckStockLocateByPage(string orderNo, string enabled, int factoryId, Pager pager, String direction, String sort)
        {
            Hashtable result = new Hashtable();

            String strWhere = " 1=1 ";
            if (orderNo != null && !orderNo.Trim().Equals(""))
            {
                strWhere += "and a.order_no = '" + orderNo.Trim() + "'";
            }

            strWhere += " and a.factory_id = " + factoryId + " ";
            String orderBy = "";
            if (sort != null && !"".Equals(sort.Trim()))
            {
                orderBy += typeof(WmsCheckStockLocate).GetEntityColumnName(sort.Trim()) + " " + direction;
            }
            else
            {
                orderBy += typeof(WmsCheckStockLocate).GetEntityColumnName("ruid") + " " + direction;
            }

            return repository.getCheckStockLocateByPage(pager.pageSize, pager.pageNo, strWhere, orderBy);

        }
        public List<WmsCheckStockRange> getCheckStockRangeByPage(string orderNo, string enabled, int factoryId, Pager pager, String direction, String sort)
        {
            Hashtable result = new Hashtable();

            String strWhere = " 1=1 ";
            if (orderNo != null && !orderNo.Trim().Equals(""))
            {
                strWhere += "and a.order_no = '" + orderNo.Trim() + "'";
            }

            strWhere += " and a.factory_id = " + factoryId + " ";
            String orderBy = "";
            if (sort != null && !"".Equals(sort.Trim()))
            {
                orderBy += typeof(WmsCheckStockRange).GetEntityColumnName(sort.Trim()) + " " + direction;
            }
            else
            {
                orderBy += typeof(WmsCheckStockRange).GetEntityColumnName("ruid") + " " + direction;
            }

            return repository.getCheckStockRangeByPage(pager.pageSize, pager.pageNo, strWhere, orderBy);

        }
        public List<WmsCheckStock> getCheckStockByOrderNo(string orderNo)
        {
            return repository.getCheckStockByOrderNo(orderNo);
        }
        public List<KeyValueResult> GetErpwarehouse(int factoryId)
        {
            return repository.GetErpwarehouse(factoryId);
        }
        public List<MultiKeyValueResult> GetMultiErpwarehouse(int factoryId)
        {
            return repository.GetMultiErpwarehouse(factoryId);
        }
        public List<MultiKeyValueResult> GetMultiPartNo(int factoryId)
        {
            return repository.GetMultiPartNo(factoryId);
        }
        public List<MultiKeyValueResult> GetMultiWarehouse(int factoryId)
        {
            return repository.GetMultiWarehouse(factoryId);
        }
        public List<MultiKeyValueResult> GetMultiZone(int factoryId)
        {
            return repository.GetMultiZone(factoryId);
        }
        public List<MultiKeyValueResult> GetMultiLocate(int factoryId)
        {
            return repository.GetMultiLocate(factoryId);
        }
        public List<KeyValueResult> GetMoveOrderType(int factoryId)
        {
            return repository.GetMoveOrderType(factoryId);
        }

        public List<KeyValueResult> GetCheckStockType(int factoryId)
        {
            return repository.GetCheckStockType(factoryId);
        }
        public List<KeyValueResult> GetCheckStockStatus(int factoryId)
        {
            return repository.GetCheckStockStatus(factoryId);
        }
        public List<KeyValueResult> getSelectFactory()
        {
            return repository.getSelectFactory();
        }

        public List<KeyValueResult> getSelectWarehouse(int factoryId)
        {
            return repository.getSelectWarehouse(factoryId);
        }
        public List<SysWarehouse> getSelectWarehouse(string warehouseid)
        {
            return repository.getSelectWarehouse(warehouseid);
        }
        public List<SysZone> getSelectZone(string zoneid)
        {
            return repository.getSelectZone(zoneid);
        }
        public List<SysLocate> getSelectLocate(string locateid)
        {
            return repository.getSelectLocate(locateid);
        }
        public List<KeyValueResult> getSelectZone(int factoryId)
        {
            return repository.getSelectZone(factoryId);
        }
        public List<KeyValueResult> getSelectVendor(int factoryId)
        {
            return repository.getSelectVendor(factoryId);
        }

        public List<SysVendor> getSelectVendor(string vendorId)
        {
            return repository.getSelectVendor(vendorId);
        }
        public List<KeyValueResult> GetPart(int factoryId)
        {
            return repository.GetPart(0, factoryId);
        }
        public List<SysPart> GetPartList(string partNo)
        {
            return repository.GetPartList(0, partNo);
        }
        public List<SysPart> GetPart(string partId)
        {
            return repository.GetPart(0, partId);
        }

        //根据选择库区筛选零件号
        public List<SysPart> getPartNoByZoneIds(string zoneIds)
        {
            if (!string.IsNullOrEmpty(zoneIds))
            {
                zoneIds = zoneIds.Substring(0, zoneIds.Length - 1);
            }
            return this.repository.getPartNoByZoneIds(zoneIds);
        }

        //根据选择库位筛选零件号
        public List<SysPart> getPartNoByLocateIds(string locateIds)
        {
            if (!string.IsNullOrEmpty(locateIds))
            {
                locateIds = locateIds.Substring(0, locateIds.Length - 1);
            }
            return this.repository.getPartNoByLocateIds(locateIds);
        }

        public List<SysPart> getPartNoByZoneIdsOrLocateIds(string zoneIds, string locateIds)
        {
            if (!string.IsNullOrEmpty(zoneIds))
            {
                zoneIds = zoneIds.Substring(0, zoneIds.Length - 1);
            }

            if (!string.IsNullOrEmpty(locateIds))
            {
                locateIds = locateIds.Substring(0, locateIds.Length - 1);
            }
            return this.repository.getPartNoByZoneIdsOrLocateIds(zoneIds, locateIds);
        }

        public List<SysPart> getPartNoByZoneId(string zoneId)
        {

            return this.repository.getPartNoByZoneId(zoneId);
        }
        public List<SysPart> getPartNoByLocateId(string locateId)
        {
            return this.repository.getPartNoByLocateId(locateId);
        }

        public Hashtable GetPartByPage(string partNo, string enabled, int factoryId, Pager pager, String direction, String sort)
        {
            Hashtable result = new Hashtable();

            String strWhere = " 1=1 ";
            if (partNo != null && !partNo.Trim().Equals(""))
            {
                strWhere += "and a.part_no like '%" + partNo.Trim() + "%'";
            }


            strWhere += " and a.factory_id = " + factoryId + " ";
            String orderBy = "";
            if (sort != null && !"".Equals(sort.Trim()))
            {
                orderBy += typeof(SysPart).GetEntityColumnName(sort.Trim()) + " " + direction;
            }
            else
            {
                orderBy += typeof(SysPart).GetEntityColumnName("partId") + " " + direction;
            }

            return repository.GetPartByPage(pager.pageSize, pager.pageNo, strWhere, orderBy);
        }

        public Hashtable GetZoneByPage(string zoneName, string warehouse, string erpWarehouse, string enabled, int factoryId, Pager pager, String direction, String sort)
        {
            Hashtable result = new Hashtable();

            String strWhere = " 1=1 ";
            if (zoneName != null && !zoneName.Trim().Equals(""))
            {
                strWhere += "and a.zone_name like '%" + zoneName.Trim() + "%'";
            }
            if (warehouse != null && !warehouse.Trim().Equals(""))
            {
                strWhere += "and a.warehouse_id in (" + warehouse.Trim() + ")";
            }
            if (erpWarehouse != null && !erpWarehouse.Trim().Equals(""))
            {
                strWhere += "and a.erp_Warehouse in (" + erpWarehouse.Trim() + ")";
            }

            strWhere += " and a.factory_id = " + factoryId + " ";
            String orderBy = "";
            if (sort != null && !"".Equals(sort.Trim()))
            {
                orderBy += typeof(SysZone).GetEntityColumnName(sort.Trim()) + " " + direction;
            }
            else
            {
                orderBy += typeof(SysZone).GetEntityColumnName("ZoneName") + " " + direction;
            }

            return repository.GetZoneByPage(pager.pageSize, pager.pageNo, strWhere, orderBy);
        }
        public Hashtable GetLocateByPage(string locateName, string warehouse, string enabled, int factoryId, Pager pager, String direction, String sort)
        {
            Hashtable result = new Hashtable();

            String strWhere = " 1=1 ";
            if (locateName != null && !locateName.Trim().Equals(""))
            {
                strWhere += "and a.locate_name like '%" + locateName.Trim() + "%'";
            }
            if (warehouse != null && !warehouse.Trim().Equals(""))
            {
                strWhere += "and a.warehouse_id in (" + warehouse.Trim() + ")";
            }

            strWhere += " and a.factory_id = " + factoryId + " ";
            String orderBy = "";
            if (sort != null && !"".Equals(sort.Trim()))
            {
                orderBy += typeof(SysLocate).GetEntityColumnName(sort.Trim()) + " " + direction;
            }
            else
            {
                orderBy += typeof(SysLocate).GetEntityColumnName("locateName") + " " + direction;
            }

            return repository.GetLocateByPage(pager.pageSize, pager.pageNo, strWhere, orderBy);
        }
        public Hashtable getCheckStock(String orderNo)
        {
            List<WmsCheckStock> dt = repository.getCheckStockByOrderNo(orderNo);
            Hashtable result = new Hashtable();
            result.Add("orderType", dt[0].OrderType);
            result.Add("partNo", dt[0].PartNo);
            result.Add("enabled", dt[0].Enabled);
            return result;
        }
        /// <summary>
        /// 保存菜单数据
        /// </summary>
        /// <param name="htParams"></param>
        /// <returns></returns>
        public bool saveCycleCountManage(WmsCheckStock checkStockParams, List<WmsCheckStockPart> checkStockPartParams, List<WmsCheckStockZone> checkStockZoneParams, List<WmsCheckStockLocate> checkStockLocateParams, List<WmsCheckStockRange> checkStockRangeParams)
        {
            return repository.saveCycleCountManage(checkStockParams, checkStockPartParams, checkStockZoneParams, checkStockLocateParams, checkStockRangeParams);
        }

        public Hashtable onAdjustment(String orderNo, string userId)
        {
            orderNo = orderNo.Substring(0, orderNo.Length - 1);
            return this.repository.onAdjustment(orderNo, userId);
        }
        public Hashtable onNoAdjustment(String orderNo, string userId)
        {
            orderNo = orderNo.Substring(0, orderNo.Length - 1);
            return this.repository.onNoAdjustment(orderNo, userId);
        }

        /// <summary>
        /// 开始盘点
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        public Hashtable onBeginCycle(String orderNo, string userId)
        {
            orderNo = orderNo.Substring(0, orderNo.Length - 1);
            return this.repository.onBeginCycle(orderNo, userId);
        }

        //关闭盘点
        public Hashtable onCloseCycle(String orderNo, string userId)
        {
            orderNo = orderNo.Substring(0, orderNo.Length - 1);
            return this.repository.onCloseCycle(orderNo, userId);
        }

        public Hashtable onCompleteCycle(string orderNo, string userId)
        {
            orderNo = orderNo.Substring(0, orderNo.Length - 1);
            return this.repository.onCompleteCycle(orderNo, userId);
        }

        public Hashtable onReplay(string orderNo, string userId)
        {
            orderNo = orderNo.Substring(0, orderNo.Length - 1);
            return this.repository.onReplay(orderNo, userId);
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int onClose(String ids, string userId)
        {
            ids = ids.Substring(0, ids.Length - 1);
            return this.repository.onClose(ids, userId);
        }

        /// <summary>
        /// 启用
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int EnableData(String ids, string userId)
        {
            ids = ids.Substring(0, ids.Length - 1);
            return this.repository.EnableData(ids, userId);
        }

        /// <summary>
        /// 禁用
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DisableData(String ids, string userId)
        {
            ids = ids.Substring(0, ids.Length - 1);
            return this.repository.DisableData(ids, userId);
        }

        //导出
        public List<CycleCountList> getCheckStockListByExportData(string orderNo)
        {
            String strWhere = " 1=1 ";
            if (orderNo != null && !orderNo.Trim().Equals(""))
            {
                strWhere += "and a.order_no = '" + orderNo.Trim() + "'";
            }

            return repository.getCheckStockListByExportData(strWhere);
        }
    }
}